home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 301-325 / disk_321 / ifs / source / getfile.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  16KB  |  522 lines

  1. /************************************************************************
  2. req.c
  3.     This file contains a general-purpose <requester> that
  4. will prompt the user for a filename input.
  5.     The program actually uses a window instead of a 'Requester'
  6. for greater flexibility. It will take control of your window's
  7. IDCMP Port when called.
  8.  
  9. ***    This material is copyright (c) 1986 by C. Heath of Microsmiths, Inc.
  10. Permission is granted to use these files in any way with the following
  11. exceptions:
  12.  
  13. 1) The files shall not be posted on any telecommunications service, public
  14. or private, except for BIX until January 15, 1987.
  15.  
  16. 2) The files may only be distributed in archive format, with no modifications.
  17. If you make any improvements on the file requester and would like to
  18. generally distribute them, please contact "cheath" on BIX, or write to:
  19.     Microsmiths Inc, PO Box 561, Cambridge, MA 02140
  20.  
  21. 3) The requester may be used in any commercial product, but must be in
  22. object code format.  You are free to make modifications for use in your
  23. product.  Permission is granted to Lattice, Inc, and to Manx, Inc, to
  24. include the source files in archive format.
  25.  
  26.     Thank you, and enjoy.
  27.         ...cheath
  28.  
  29. 5/1/89 -- Modified by Glen Fullmer to clean up compiler warnings.
  30.  
  31. ************************************************************************/
  32.  
  33. #include "standard.h"
  34. #include "safeclose.h"
  35.  
  36. #define FAST register
  37. #define NL NULL
  38.  
  39. extern char *dmore(), *dinit();
  40.  
  41. static struct FileLock      *pdir = NL;
  42. static struct FileInfoBlock *dir_info;
  43.  
  44. static struct Window   *eW;        /* Parent Window. Uck    */
  45.  
  46. static struct TextAttr MyFont = {"topaz.font",TOPAZ_EIGHTY,FS_NORMAL,FPF_ROMFONT };
  47.  
  48. /* FGAD requires a few unique gadget-ids, origined at FGAD    */
  49. /* In the unlikely event this conflicts with any of your gadgets, */
  50. /* change this equate to allow 16 contiguous unique ID's    */
  51.  
  52. #define FGAD    0x76c0L
  53.  
  54. #define FCHARS    32L    /* Number of chars allowed in file name    */
  55.  
  56. #define DIR_SIZ    50L    /* Number of chars in a dir name...    */
  57. #define MAX_STR    DIR_SIZ+2L
  58.  
  59. #define DENTS    5L        /* Number of entries on screen   */
  60. /* It's not as simple as changing 'DENTS'...      */
  61.  
  62. #define DSIZE    FCHARS+1L    /* Size of a directory entry   */
  63.  
  64. #define    DBUFSIZ    3000L    /* Number of bytes to allocate for all ents */
  65.  
  66. #define BCOL    1L    /* Background color    */
  67. #define FCOL    2L    /* Foreground color    */
  68.  
  69. #define RHGHT    120L
  70. #define RWDTH    320L
  71.  
  72. #define ZWDTH    270L
  73.  
  74. static char    ubuf[MAX_STR];        /* Undo buffer    */
  75.  
  76. void free_pdir();            /* dummy forwards to avoid compiler */
  77. void cxxx();                /* warnings     */
  78. void rfnam();
  79. void notify();
  80.  
  81. static struct dirent { struct dirent *next; BOOL isfile; char dE[DSIZE+2]; };
  82. static struct dirent *FirstEntry;
  83. static struct dirent *NextEntry;
  84.  
  85. static struct dirhead { struct dirent *next; };
  86. static struct dirhead ListHead;
  87.  
  88. static long   curent,maxent;
  89. static BOOL   more;
  90.  
  91. static struct Window    *wRq = NL;        /* Requester window   */
  92. static struct RastPort    *wRp;
  93.  
  94. /* Requester "Hailing" prompt */
  95. static struct IntuiText oTxt = {2,2,JAM1,10,11,NL, NL ,NL};
  96.  
  97. static struct IntuiText saydir = {0,1,JAM2,0,1,&MyFont,(UBYTE *)"(dir) ",NL};
  98.  
  99. static struct IntuiText rname[DENTS] = { /* File name list */
  100.    {2,1,JAM2,48,1,NL,NL, NL},
  101.    {2,1,JAM2,48,1,NL,NL, NL},
  102.    {2,1,JAM2,48,1,NL,NL, NL},
  103.    {2,1,JAM2,48,1,NL,NL, NL},
  104.    {2,1,JAM2,48,1,NL,NL, NL} };
  105.  
  106. /* Display for file name ... */
  107.  
  108. static SHORT oXY2[] = {-2,-2, RWDTH-78,-2, RWDTH-78,9, -2,9, -2,-2};
  109. static struct Border thebd = {0,0, 2,3,JAM1, 5,oXY2, NL};
  110.  
  111. static struct IntuiText otxt = {2,2,JAM1,-40,0,&MyFont,(UBYTE *)"file",NL};
  112. static struct StringInfo osx = { NL ,ubuf, NL,DSIZE,NL,NL,NL,NL,NL,NL,NL,NL,NL};
  113. static struct Gadget ogx = { NL, 60,RHGHT-35,RWDTH-80 ,10,     /* File name gadget */
  114.    GADGHCOMP, RELVERIFY , STRGADGET,
  115.    (APTR)&thebd,NL,&otxt,NL,(APTR)&osx, FGAD+11,NL };
  116.  
  117. static struct Gadget oga = { &ogx, 10,70, ZWDTH,10,   /* Gadgets For   */
  118.    GADGHCOMP, RELVERIFY, BOOLGADGET,     /* Directory entries   */
  119.    NL,NL, &rname[4] ,NL,NL, FGAD+10,NL };
  120. static struct Gadget og9 = {&oga, 10,60, ZWDTH,10,
  121.    GADGHCOMP, RELVERIFY, BOOLGADGET,
  122.    NL,NL, &rname[3] ,NL,NL, FGAD+9,NL };
  123. struct Gadget og8 = {&og9, 10,50, ZWDTH,10,
  124.    GADGHCOMP, RELVERIFY, BOOLGADGET,
  125.    NL,NL, &rname[2] ,NL,NL, FGAD+8,NL };
  126. static struct Gadget og7 = {&og8, 10,40, ZWDTH,10,
  127.    GADGHCOMP, RELVERIFY, BOOLGADGET,
  128.    NL,NL, &rname[1] ,NL,NL, FGAD+7,NL };
  129. static struct Gadget og6 = {&og7, 10,30, ZWDTH,10,
  130.    GADGHCOMP, RELVERIFY, BOOLGADGET,
  131.    NL,NL, &rname[0] ,NL,NL, FGAD+6,NL };
  132.  
  133.  
  134. /* Gadjets for requester  */
  135.  
  136. static SHORT oXY3[] = {0,0, 50,0, 50,9, 0,9, 0,0};
  137. static SHORT oXY4[] = {2,-2, 48,-2, 52,2, 52,7, 48,11, 2,11, -2,7, -2,2, 2,-2};
  138. static struct Border obd2 = {0,0, 2,3,JAM1, 9,oXY4, NL};
  139. static struct Border obd1 = {0,0, 3,2,JAM1, 5,oXY3, &obd2};  /* OTAY / CANCEL box */
  140.  
  141. static struct IntuiText ot1 = {0,0,JAM1,1,1,&MyFont,(UBYTE *)"  OK  ",NL};
  142. static struct IntuiText ot2 = {0,0,JAM1,1,1,&MyFont,(UBYTE *)"Cancel",NL};
  143.  
  144. static struct IntuiText dtxt = {2,2,JAM1,-60,0,NL,(UBYTE *)"drawer",NL};
  145. static struct StringInfo os5 = { NL ,ubuf, NL,DIR_SIZ,NL,NL,NL,NL,NL,NL,NL,NL,NL};
  146. static struct Gadget og5 = { &og6, RWDTH/2-80,19,190,10,     /* Directory */
  147.    GADGHCOMP, RELVERIFY, STRGADGET,
  148.    NL,NL,&dtxt,NL,(APTR)&os5, FGAD+5,NL };
  149.  
  150. static struct Image   cc_img;
  151. static struct PropInfo   cc_prop = {AUTOKNOB | FREEVERT, 0,0, 0,0x1000,0,0,0,0,0,0 };
  152. static struct Gadget og3 = { &og5,RWDTH-39,20,20,60,      /* Scroll Bar   */
  153.     GADGHCOMP,GADGIMMEDIATE | FOLLOWMOUSE, PROPGADGET,
  154.     (APTR)&cc_img,NL,NL,NL,(APTR)&cc_prop,FGAD+3,NL };
  155.  
  156. static struct Gadget og2 = { &og3, RWDTH-70,RHGHT-20, 50,10,  /* CANCEL */
  157.    GADGHCOMP,  RELVERIFY, BOOLGADGET,
  158.    (APTR)&obd1,NL, &ot2,NL,NL, FGAD+2,NL };
  159.  
  160. static struct Gadget og1 = { &og2, 20,RHGHT-20, 50,10,      /* OTAY   */
  161.    GADGHCOMP,        /* Flags    */
  162.    RELVERIFY,        /* Activation    */
  163.    BOOLGADGET,
  164.    (APTR)&obd1,NL,    /* GadgetRender, SelectRender    */
  165.    &ot1,NL,NL,        /* IntuiText, MutualExclude,SpecialInfo   */
  166.    FGAD+1,NL };        /* Gadget Id, UserData    */
  167.  
  168. /* Open a requester "Window" */
  169.  
  170. static struct NewWindow NewFiles = {
  171.    160, 30, RWDTH,RHGHT, BCOL,FCOL, NL, /* Fill in AFTER opening ... */
  172.    SMART_REFRESH | ACTIVATE | RMBTRAP | WINDOWDRAG,
  173.    &og1,NL,NL, NL,
  174.    NL, RWDTH,RHGHT,RWDTH,RHGHT, WBENCHSCREEN };
  175.  
  176. IMPORT struct Library    *IntuitionBase;
  177.  
  178. /***************************************************
  179. *  get_fname(window,screen,hail,ddef,ddir);
  180. *
  181. *   Displays a window/requester that
  182. * gets a file name for device,directory,default file, extension
  183. *
  184. *   Calling args:
  185. * window:   Window making the request
  186. * screen:   Screen, if NULL assummed workbench
  187. * hail:   Text prompt at top of requester
  188. * ddef:   Input default file-name. Has NO DIRECTORY OR EXTENSION.
  189. * ddir:   Directory of file, may be null
  190.  
  191. /* Set a file-requester with prompt 'hail'   */
  192.  
  193. char *get_fname(cW,screen,hail,ddef,ddir)
  194.    struct Window *cW;        /* Calling Window   */
  195.    struct Screen *screen;    /* screen .... if null assumed workbench */
  196.    UBYTE    *hail;        /* Hailing prompt   */
  197.    char        *ddef;        /* Proable file-name   */
  198.    char        *ddir;        /* Directory in which to search   */
  199.    {
  200.    FAST struct IntuiMessage *imes;   /* Wait for message in HERE   */
  201.    FAST struct Gadget    *igad;   /* Get Gadjet Mumbo Jumbo   */
  202.    FAST long    i,class;
  203.    FAST TEXT    *pnam;
  204.  
  205.    FAST char    *retval;
  206.    FAST BOOL    dir_flag = FALSE;
  207.    FAST BOOL    keepon;
  208.  
  209.    if ( ! (eW = cW) )   return(NL);
  210.  
  211.    osx.Buffer = ddef;    /* Set default file name   */
  212.    os5.Buffer = ddir;    /* Set default device name   */
  213.  
  214.    for ( i=0; i<DENTS; i++)
  215.       {
  216.       rname[i].IText = "";
  217.       rname[i].NextText = NL;
  218.       };
  219.  
  220.    NewFiles.Title = eW->Title;
  221.    if ((dir_info = (struct FileInfoBlock *) 
  222.                    AllocMem((long)sizeof(struct FileInfoBlock),0L)) == NULL)
  223.       return(NL);
  224.  
  225.    if (screen)        /* User supplied a screen */
  226.       {
  227.       NewFiles.Type = CUSTOMSCREEN;
  228.       NewFiles.Screen = screen;
  229.       }
  230.  
  231.    if ( ! (FirstEntry = (struct dirent *)AllocMem((long)DBUFSIZ,0L)) ||
  232.         ! (wRq = (struct Window *)OpenWindow( &NewFiles )) )
  233.       {
  234.       if ( FirstEntry )   FreeMem(FirstEntry,(long)DBUFSIZ);
  235.       /* notify("Can't Open Requester..."); */
  236.       FreeMem(dir_info,(long)sizeof(struct FileInfoBlock));
  237.       return(NL);
  238.       }
  239.  
  240.    /* Set up directory, notify any errors...   */
  241.    if ( pnam = (char *)dinit(ddir) )   notify(pnam);
  242.  
  243.  
  244.    /* This optional line will activate a string gadget    */
  245.    if ( IntuitionBase->lib_Version > 32 )
  246.     {
  247.     ActivateGadget(&ogx,wRq,0L);
  248.     }
  249.  
  250.    wRp = wRq->RPort;
  251.  
  252.    wRq->UserPort = eW->UserPort;
  253.    ModifyIDCMP(wRq,(long)(MOUSEBUTTONS | GADGETDOWN | GADGETUP | MOUSEMOVE));
  254.  
  255.    SetAPen(wRp,1L);
  256.    RectFill(wRp,4L,10L,(long)(RWDTH-5),(long)(RHGHT-4));
  257.  
  258.    oTxt.IText = hail;   /* Set calling arg   */
  259.    oTxt.LeftEdge = (RWDTH - IntuiTextLength(&oTxt)) >> 1L;
  260.    PrintIText(wRp,&oTxt,0L,0L);
  261.  
  262.    RefreshGadgets(&og1,wRq,(long)NL);
  263.    for ( retval= NL, keepon=TRUE; keepon ; )
  264.       {
  265.       while ( ! (imes=(struct IntuiMessage *)GetMsg(wRq->UserPort)) )
  266.       {
  267.          if ( dir_flag )
  268.             {
  269.             i = (maxent-DENTS) * cc_prop.VertPot / MAXBODY;
  270.             if ( i > (maxent-DENTS) )
  271.             i = maxent-DENTS;
  272.             if ( i <0 )   i = 0;
  273.             curent = i;
  274.             cxxx();
  275.             dir_flag = FALSE;
  276.             }
  277.          if ( more )
  278.             {
  279.             if (pnam = (char *)dmore())   /* Continue to read the directory */
  280.                notify(pnam);      /* Yucko error   */
  281.             if ( maxent <= DENTS ) dir_flag = TRUE;
  282.             }
  283.          else    WaitPort(wRq->UserPort);
  284.          }
  285.       igad = (struct Gadget *)imes->IAddress;
  286.       class = imes->Class;
  287.       ReplyMsg(imes);
  288.  
  289.       switch (class)
  290.          {
  291.          case MOUSEMOVE:      dir_flag = TRUE;   break;
  292.       
  293.          case GADGETUP:
  294.          case GADGETDOWN:
  295.             switch ( i = igad->GadgetID)
  296.             {
  297.             case FGAD+6:
  298.             case FGAD+7:
  299.             case FGAD+8:
  300.             case FGAD+9:
  301.             case FGAD+10:       /* Replace file or directory name   */
  302.                pnam = rname[i - (FGAD+6)].IText;
  303.                if ( rname[igad->GadgetID - (FGAD+6)].NextText == NL )
  304.                   {
  305.                   RemoveGadget(wRq,&ogx);
  306.                   for (i=0; i<DSIZE; i++)      ddef[i] = *pnam++;
  307.                   AddGadget(wRq,&ogx,-1L);
  308.                   RefreshGadgets(&ogx,wRq,(long)NL);
  309.                   break;
  310.                   }
  311.                   else
  312.                   {
  313.                   RemoveGadget(wRq,&og5);
  314.                   rfnam(ddir,pnam);
  315.                   AddGadget(wRq,&og5,-1L);
  316.                   RefreshGadgets(&og5,wRq,(long)NL);
  317.                   }
  318.             case FGAD+5:
  319.                if ( pnam = (char *)dinit(ddir) )
  320.                   notify(pnam);
  321.             case FGAD+3:
  322.                dir_flag = TRUE;
  323.                break;
  324.  
  325.             case FGAD+11:      /* Name gadget, OTAY gadget   */
  326.             case FGAD+1:
  327.                retval = ddef;
  328.             case FGAD+2:      /* Cancel gadget   */
  329.                keepon = FALSE;
  330.             }
  331.          }
  332.       }
  333.  
  334.       FreeMem(FirstEntry,(long)DBUFSIZ );
  335.       FreeMem(dir_info,(long)sizeof(struct FileInfoBlock));
  336.       free_pdir();
  337.  
  338.       CloseWindowSafely(wRq);
  339.       return(retval);
  340.    }
  341.  
  342. static void free_pdir()
  343.    {
  344.    if ( pdir )
  345.       {
  346.       UnLock(pdir);
  347.       pdir = NL;
  348.       }
  349.    }
  350.  
  351. /*****************************************************************
  352. * dinit()
  353. *   Initialize the fib for directory muck.  Null return
  354. * is good, else return is a pointer to an error string      */
  355.  
  356. static char *dinit(subdir)
  357.    char *subdir;
  358.    {
  359.    more = FALSE;
  360.    curent = maxent = 0;
  361.  
  362.    NextEntry = FirstEntry;      /* Allocate from here   */
  363.    ListHead.next = NL;          /* Clear the boogie     */
  364.  
  365.    free_pdir();   /* Unlock any old lock... */
  366.  
  367.    if (! (pdir=(struct FileLock *)Lock(subdir,(ULONG)ACCESS_READ)) )
  368.       return("Wrong Diskette?");
  369.    if ( ! Examine(pdir, dir_info) )   return("Wierd Disk Error");
  370.    if ( dir_info->fib_DirEntryType < 0L )   return("Bizzare Alert!!");
  371.  
  372.    more = TRUE;
  373.    return(dmore());
  374.    }
  375.  
  376.  
  377. static char *dmore()
  378.    {
  379.    FAST struct dirent   *p_D = NextEntry;
  380.    FAST struct dirent   *ptr = (struct dirent *)&ListHead;
  381.    FAST struct dirent   *plink;
  382.  
  383.    FAST   TEXT   *p_mung;
  384.  
  385.    FAST long    i;
  386.  
  387.    if ( ! more )   return(NL);
  388.  
  389.    if ( ExNext( pdir, dir_info ) )
  390.       {
  391.  
  392.  
  393.       if ( (ULONG)p_D >=
  394.          ((ULONG)FirstEntry + (ULONG)DBUFSIZ - (ULONG)sizeof(struct dirent)) )
  395.          {
  396.          more = FALSE;
  397.          return("Directory Truncated!");
  398.          }
  399.  
  400.  
  401.       /* Here you can add a file/directory filter   */
  402.       /* filename text string is at &p_D->dE[0   */
  403.       p_D->isfile = ( dir_info->fib_DirEntryType < 0L );
  404.  
  405.       p_mung = &p_D->dE[0];
  406.       for ( i=0; i<FCHARS; i++)
  407.          if ( ! (*p_mung++ = dir_info->fib_FileName[i]) )   break;
  408.  
  409.       i = (long)p_mung;
  410.       NextEntry = (struct dirent *)( (i+5L) & ~3L );
  411.  
  412.       for ( i=maxent++; i>=0; i--)
  413.          {
  414.          if ( ! (plink = ptr->next)  )   break;
  415.          if ( alpha_lower(p_D,plink) )   break;
  416.          ptr = plink;
  417.          }
  418.       p_D->next = plink;
  419.       ptr->next = p_D;
  420.  
  421.       return(NL);
  422.       }
  423.    else return ( IoErr() == ERROR_NO_MORE_ENTRIES) ?
  424.        (char *)(more = (long)NL) : "Error Reading Directory!!!";
  425.    }
  426.  
  427.  
  428. /* dedicated alphabetizing function for dmore()   */
  429.  
  430. static alpha_lower(snew,sold)
  431.    struct dirent *snew,*sold;
  432.    {
  433.    FAST struct dirent *pnew = snew;
  434.    FAST TEXT *ps1,*ps2, c,d;
  435.  
  436.    if ( pnew->isfile == sold->isfile)
  437.       {
  438.       ps1 = &pnew->dE[0];
  439.       ps2 = &sold->dE[0];
  440.       while ( (c=*ps1++) )
  441.          {
  442.          if ( c > (d=*ps2++) )   return(FALSE);
  443.          else if ( c < d )      break;
  444.          }
  445.       return(TRUE);
  446.       }
  447.    return(pnew->isfile);
  448.    }   
  449.  
  450.  
  451.  
  452. /* Display directory stuff   */
  453.  
  454. static void cxxx()
  455.    {
  456.    FAST long   i,new;
  457.    FAST long   x,y;
  458.    FAST struct dirent *ohboy = (struct dirent *)&ListHead;
  459.  
  460.    new = curent;
  461.    for ( i=0; i<new; i++)   ohboy = ohboy->next;
  462.  
  463.    y = 20L;
  464.    for (i=0; i<DENTS; i++)
  465.       {
  466.       y += (x=10);
  467.       rname[i].NextText = NL;
  468.       rname[i].IText = "";
  469.       rname[i].LeftEdge = 0;
  470.       if ( (new+i) < maxent )
  471.          {
  472.          ohboy = ohboy->next;
  473.          rname[i].IText = &ohboy->dE[0];
  474.          if ( ohboy->isfile )   PrintIText(wRp,&rname[i],10L,y);
  475.          else
  476.             {
  477.             rname[i].LeftEdge = 48;
  478.             PrintIText(wRp,&saydir,10L,y);
  479.             PrintIText(wRp,&rname[i],10L,y);
  480.             rname[i].NextText = &saydir;
  481.             }
  482.          x = wRp->cp_x;
  483.          }
  484.       if ( x < ZWDTH+10 )   RectFill(wRp,x,y,(long)(ZWDTH+10),(long)(y+8L));
  485.       }
  486.    }
  487.  
  488.  
  489. /**************************************************
  490. * rfnam()
  491. *   Combines dir, plus name into dir   */
  492.  
  493. static void rfnam(dir,fil_nam)
  494.    char *dir,*fil_nam;
  495.    {
  496.    FAST char   *pdst = dir;
  497.    FAST char   *psrc = fil_nam;
  498.    FAST char   c = ':';
  499.  
  500.    while ( *pdst )
  501.       c = *pdst++;
  502.    if ( c != ':')   *pdst++ = '/';
  503.  
  504.    while ( *pdst++ = *psrc++ )
  505.       ;
  506.    }
  507.  
  508. static struct IntuiText b_txt = {0,1,JAM2, 5,20,NL,NL,    NL};
  509. static struct IntuiText p_txt = {0,1,JAM2, 5,3,NL,"OK", NL};
  510.  
  511. /****************************************************************/
  512. /* notify(txt)                     */
  513. /*   Prompts for Yes/No response            */
  514.  
  515. static void notify(txt)
  516.    char *txt;
  517.    {
  518.    b_txt.IText = txt;
  519.    AutoRequest(wRq,&b_txt,0L,&p_txt,0L,0L,
  520.       (long)(IntuiTextLength(&b_txt)+50L),70L);
  521.    }
  522.